﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;

namespace ReconUI
{
    public partial class SearchRoleCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                control.Value = Request.QueryString["textbox"].ToString();
                //path.Value = Request.QueryString["PageRequest"].ToString();
                BindGrid();
                txtRoleCode.Focus();

            }
        }

        #region User Defined Function

        public void BindGrid()
        {
            DataTable dtUserDetails = new DataTable();
            DBFactory objDBfactory = new DBFactory();

            try
            {

                if (txtRoleCode.Text.Trim() == "")
                {
                    dtUserDetails = objDBfactory.GetRoleDetailsByRoleId(Convert.ToString(control.Value).Trim(),2);

                    if ((dtUserDetails) == null)
                    {
                        lblRolecnt.Text = "No Records Found";
                    }
                    else if (dtUserDetails.Rows.Count >= 1)
                    {
                        gvRole.DataSource = dtUserDetails;
                        gvRole.DataBind();
                        lblRolecnt.Text = ((DataTable)gvRole.DataSource).Rows.Count.ToString() + " records found.";
                    }
                    else
                    {
                        lblRolecnt.Text = "No Records Found";
                    }
                }
                else
                {
                    dtUserDetails = objDBfactory.GetRoleDetailsByRoleId(Convert.ToString(txtRoleCode.Text).Trim(),2);

                    if ((dtUserDetails) == null)
                    {
                        lblRolecnt.Text = "No Records Found";
                    }
                    else if (dtUserDetails.Rows.Count >= 1)
                    {
                        gvRole.DataSource = dtUserDetails;
                        gvRole.DataBind();
                        lblRolecnt.Text = ((DataTable)gvRole.DataSource).Rows.Count.ToString() + " records found.";
                    }
                    else
                    {
                        lblRolecnt.Text = "No Records Found";
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((dtUserDetails) != null)
                {
                    dtUserDetails.Dispose();
                }
                dtUserDetails = null;
            }
        }

        #endregion

        //protected void gvRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    gvRole.PageIndex = e.NewPageIndex;
        //    BindGrid();
        //}

        protected void gvRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            string roleCode;
            roleCode = ((LinkButton)gvRole.SelectedRow.FindControl("lbtnRoleCode")).Text;
            string strScript = "<script>";
            strScript += "window.opener.document.all('ctl00$BodyHolder$txtRoleCode').value = '" + roleCode + "';";
            strScript += "self.close();</script>";
            Response.Write(strScript);
        }

        protected void gvRole_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }


    }
}